package com.example.hyg_veintype.activity.result.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.data.local.chart.ChartBean;
import com.example.hyg_veintype.base.baseadapter.BaseHolder;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;
import com.example.hyg_veintype.utils.chartUtil.OptimalChartUtils;
import com.github.mikephil.charting.charts.LineChart;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author: yyw
 * time: 2020/12/16 15:25
 * desc: 最佳脉型列表适配器
 */
public class ChartAdapter extends BaseRecyclerAdapter<ChartBean> {

    public ChartAdapter(List<ChartBean> infos, Context context) {
        super(infos, context);
    }

    @Override
    public BaseHolder<ChartBean> getHolder(View v, int viewType) {
        return new MyHolder(v, context);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.veintype_item_chart;
    }

    class MyHolder extends BaseHolder<ChartBean> {
        TextView tvName;
        LineChart chart;

        public MyHolder(View itemView, Context context) {
            super(itemView, context);
            tvName = itemView.findViewById(R.id.tv_name);
            chart = itemView.findViewById(R.id.chart);
        }

        @Override
        public void setData(ChartBean data, int position) {
            tvName.setText(data.getName());
            //主波幅高最大的为最佳脉图
            List<Double> sortList = new ArrayList();
            sortList.add(data.getFuSd().p主波幅高);
            sortList.add(data.getZhongSd().p主波幅高);
            sortList.add(data.getChenSd().p主波幅高);
            double max = Collections.max(sortList);
            int index = sortList.indexOf(max);
            List<Integer> pointList = new ArrayList<>();
            int start;
            int end;
            int zhubofu;
            if (max == 0) {
                return;
            }
            if (index == 0) {
                pointList.addAll(data.getFuSd().listCopy.subList(data.getFuSd().myAutoStartIndex, data.getFuSd().myAutoEndIndex + 1));
                start = data.getFuSd().myAutoStartIndex;
                end = data.getFuSd().myAutoEndIndex;
                zhubofu = data.getFuSd().CBFIndexCopy;
            } else if (index == 1) {
                pointList.addAll(data.getZhongSd().listCopy.subList(data.getZhongSd().myAutoStartIndex, data.getZhongSd().myAutoEndIndex + 1));
                start = data.getZhongSd().myAutoStartIndex;
                end = data.getZhongSd().myAutoEndIndex;
                zhubofu = data.getZhongSd().CBFIndexCopy;
            } else if (index == 2) {
                pointList.addAll(data.getChenSd().listCopy.subList(data.getChenSd().myAutoStartIndex, data.getChenSd().myAutoEndIndex + 1));
                start = data.getChenSd().myAutoStartIndex;
                end = data.getChenSd().myAutoEndIndex;
                zhubofu = data.getChenSd().CBFIndexCopy;
            }
            //缩放脉点，使其在图表内均匀分布，最佳脉图
            OptimalChartUtils.setOptimalChart(pointList, chart);
        }
    }
}
